import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:sui_shou_pai/module/color.dart';
import 'package:sui_shou_pai/module/effect.dart';
import 'package:sui_shou_pai/network/http_config.dart';
import 'package:sui_shou_pai/network/http_request.dart';
import 'package:sui_shou_pai/news/bottom_tool_bar.dart';
import 'package:sui_shou_pai/record/record_comment_list.dart';
import 'package:sui_shou_pai/record/record_entity/photo_record_entity.dart';

import 'image_widget.dart';

class RecordDetail extends StatefulWidget {
  const RecordDetail({Key key}) : super(key: key);

  @override
  _RecordDetailState createState() => _RecordDetailState();
}

class _RecordDetailState extends State<RecordDetail> {
  void deleteRecord(int id) {
    HttpRequest.request(
      "$BASE_URL/photo-record/deletePhotoRecord/$id",
      method: "POST",
    ).then((value) {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    List<String> imageSplit(String image) {
      List<String> imageList = image.split(',');
      return imageList;
    }

    PhotoRecordEntity photoRecordEntityDetail =
        ModalRoute.of(context).settings.arguments;
    List<String> imageList = imageSplit(photoRecordEntityDetail.image);

    // print(imageList);
    // print(imageList.length);

    Widget getSelect(String title) {
      return Container(
        child: GestureDetector(
          onTap: () {
            int _id = int.parse(photoRecordEntityDetail.id);
            deleteRecord(_id);
            Navigator.of(context).pop("true");
            Fluttertoast.showToast(
                msg: "删除成功",
                toastLength: Toast.LENGTH_SHORT,
                gravity: ToastGravity.BOTTOM);
          },
          child: Text(
            title,
          ),
        ),
      );
    }

    return MaterialApp(
      title: '记录详情页面',
      theme: ThemeData(primaryColor: Colors.white),
      color: Colors.white,
      home: Scaffold(
          backgroundColor: Colors.white,
          appBar: AppBar(
            centerTitle: true,
            title: Text('记录详情', style: TextStyle(color: Colors.black)),
            // 自定义返回按钮
            leading: GestureDetector(
                child: Icon(Icons.arrow_back),
                onTap: () {
                  Navigator.of(context).pop();
                }),
            // 设置保存按钮
            actions: <Widget>[
              IconButton(
                  icon: Icon(Icons.check),
                  onPressed: () {
                    Fluttertoast.showToast(
                        msg: "保存成功",
                        toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.BOTTOM);
                  }),
              PopupMenuButton(
                  itemBuilder: (BuildContext context) => [
                        new PopupMenuItem(value: "选项一的内容", child: Text("更新记录")),
                        new PopupMenuItem(
                            value: "选项二的内容", child: getSelect("删除记录")),
                      ]),
            ],
          ),
          body: Column(
            children: <Widget>[
              Expanded(
                child: SingleChildScrollView(
                  padding: EdgeInsets.all(16),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      getStatus(photoRecordEntityDetail),
                      Padding(
                        padding: EdgeInsets.symmetric(vertical: 16),
                        child: Text(
                          photoRecordEntityDetail.content,
                          maxLines: 10,
                          style: TextStyle(
                            fontSize: 17,
                          ),
                        ),
                      ),
                      Container(
                        constraints: BoxConstraints(maxHeight: 300),
                        child: ListView.separated(
                          shrinkWrap: true,
                          itemCount:
                              imageList.length == 0 ? 0 : imageList.length - 1,
                          itemBuilder: (BuildContext context, int index) {
                            return Container(
                                height: 300,
                                child: ClipRRect(
                                  borderRadius: borderRadiusAll8(),
                                  child: ImageWidget(
                                      url: imageSplit(photoRecordEntityDetail
                                          .image)[index]),
                                ));
                          },
                          separatorBuilder: (BuildContext context, int index) {
                            return new Divider();
                          },
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.symmetric(vertical: 16),
                        padding:
                            EdgeInsets.symmetric(vertical: 8, horizontal: 16),
                        decoration: BoxDecoration(
                            color: greyF6F6F6,
                            borderRadius: borderRadiusAll8()),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Padding(
                              padding: EdgeInsets.symmetric(vertical: 4),
                              child: Row(
                                children: <Widget>[
                                  Icon(Icons.perm_identity_outlined,
                                      size: 20.0),
                                  SizedBox(
                                    width: 4.0,
                                  ),
                                  Text(
                                    photoRecordEntityDetail.publisherName,
                                    style: TextStyle(
                                      fontSize: 17,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            Padding(
                              padding: EdgeInsets.symmetric(vertical: 4),
                              child: Row(
                                children: <Widget>[
                                  Icon(Icons.location_on_outlined, size: 20.0),
                                  SizedBox(
                                    width: 4.0,
                                  ),
                                  Text(
                                    photoRecordEntityDetail.location,
                                    style: TextStyle(
                                      fontSize: 17,
                                    ),
                                  )
                                ],
                              ),
                            ),
                            Padding(
                              padding: EdgeInsets.symmetric(vertical: 4),
                              child: Row(
                                children: <Widget>[
                                  Icon(Icons.access_time_rounded, size: 20.0),
                                  SizedBox(
                                    width: 4.0,
                                  ),
                                  Text(
                                    photoRecordEntityDetail.updateTime,
                                    style: TextStyle(fontSize: 17),
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                      RecordCommentList(
                        photoRecordId: photoRecordEntityDetail.id,
                      ),
                    ],
                  ),
                ),
              ),
              BottomToolBar(
                  currentContentId: photoRecordEntityDetail.id,
                  currentContentType: "record"),
            ],
          )),
    );
  }
}

Widget getStatus(PhotoRecordEntity photoRecordEntity) {
  var statusColor;
  var title;
  if (photoRecordEntity.status.isNotEmpty) {
    if (photoRecordEntity.status == "1") {
      statusColor = Colors.green;
      title = "已通过";
    } else if (photoRecordEntity.status == "2") {
      statusColor = Colors.yellow;
      title = "执行中";
    } else {
      statusColor = Colors.red;
      title = "已提交";
    }
  }

  return Container(
      alignment: Alignment.center,
      height: 26,
      width: 56,
      decoration:
          BoxDecoration(borderRadius: borderRadiusAll4(), color: statusColor),
      child: Center(
          child: Text(
        title,
        style: TextStyle(color: Colors.black54, fontWeight: FontWeight.bold),
      )));
}
